local fubing = fk.CreateSkill {
  name = "lingling__fubing",
}

Fk:loadTranslationTable{
  ["lingling__fubing"] = "府兵",
  [":lingling__fubing"] = "出牌阶段限一次，你可以弃置一张【杀】，令至多三名角色各摸一张牌（若只选择一名角色，其多摸一张牌），这些角色下回合首次造成伤害后，你获得其一张牌。",

  ["#lingling__fubing"] = "府兵：弃一张【杀】令至多三名角色各摸一张牌，这些角色下回合首次造成伤害后，你获得其一张牌",
  ["@@lingling__fubing-turn"] = "府兵",
  ["#lingling__fubing-give"] = "府兵：获得 %src 一张牌",
}

fubing:addEffect("active", {
  anim_type = "support",
  prompt = "#lingling__fubing",
  card_num = 1,
  min_target_num = 1,
  max_target_num = 3,
  can_use = function(self, player)
    return player:usedSkillTimes(fubing.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).trueName == "slash" and not player:prohibitDiscard(to_select)
  end,
  target_filter = function (self, player, to_select, selected)
    return #selected < 3
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    room:sortByAction(effect.tos)
    for _, p in ipairs(effect.tos) do
      if not p.dead then
        if p ~= player then
          room:addTableMarkIfNeed(p, fubing.name, player.id)
        end
        p:drawCards(#effect.tos == 1 and 2 or 1, fubing.name)
      end
    end
    room:throwCard(effect.cards, fubing.name, player, player)
  end,
})

fubing:addEffect(fk.Damage, {
  anim_type = "control",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return table.contains(target:getTableMark("@@lingling__fubing-turn"), player.id) and not player.dead
  end,
  on_cost = function (self, event, target, player, data)
    event:setCostData(self, {tos = {target}})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:removeTableMark(target, "@@lingling__fubing-turn", player.id)
    if not target:isNude() then
      room:obtainCard(player, room:askToChooseCard(player, {
        target = target,
        flag = "he",
        skill_name = fubing.name,
        prompt = "#lingling__fubing-give:"..target.id,
      }), nil, fk.ReasonPrey, player, fubing.name)
    end
  end,
})

fubing:addEffect(fk.TurnStart, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark(fubing.name) ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@@lingling__fubing-turn", player:getMark(fubing.name))
    room:setPlayerMark(player, fubing.name, 0)
  end,
})

return fubing
